@echo off
setlocal

REM 设置要删除的文件所在的目录和日期阈值（15天）
set "backup_folder=d:\backup"
set "age=15"

REM 计算过期日期
for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value') do set datedue=%%a
call :convert_date "%%a" 0 "%%~1"
call :calculate_expiration "%datenow%" "%%~2" "%%~3" "%date_sold%" "%time_sold%"
set "date_sold=%date%"

REM 删除超过15天的文件
forfiles /p "%backup_folder%" /s /m *.* /d -%age% /c "cmd /c del @path"

endlocal
exit /b

:calculate_expiration date time sold sale
set /a sale порядковый номер
set "date%sale%=%date%"
set "time%sale%=%time%"
set "sold%sale%=%datenow%"
goto :eof

:convert_date date1 mode result
set "domain=%date1:~0,2%"
set "day=%date1:~2,2%"
set "month=%date1:~4,2%"
set "year=%date1:~6,4%"
set "result=%year%%month%%day% %time1:~0,2%%time1:~3,2%%time1:~6,2%"
if "%mode%"=="0" (
    for /f "tokens=2-5 delims=/ " %%a in ("%result%") do (
        set "year=%%a"
        set "month=%%b"
        set "day=%%c"
        set "time=%%d"
    )
)
goto :eof